﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataTableDataHistoryList", "1.0")]
    public class GetDataTableDataHistoryListAction : BaseAction<GetDataTableDataHistoryListParams, GetDataTableDataHistoryListResult>
    {
        protected override ActionResult<GetDataTableDataHistoryListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataTableDataHistoryListParams opParams)
        {
            string[] pkColumnArr = opParams.PKColumns.Split(new char[] { ',' });
            string[] pkValueArr = opParams.PKValues.Split(new char[] { ',' });

            SER.DtTableService service = new SER.DtTableService();
            ListResult<VIW.VDtTableDataHistory> serResult = service.GetCellHistories(opParams.DataTableId, pkColumnArr, pkValueArr, opParams.ColumnName);
            if (serResult.IsSuccess)
            {
                GetDataTableDataHistoryListResult actResult = new GetDataTableDataHistoryListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetDataTableDataHistoryListResult.Item item = new GetDataTableDataHistoryListResult.Item();

                    item.DataId = serResult.Data[index].DataId;
                    item.ColumnName = serResult.Data[index].ColumnName;
                    item.IsLatest = serResult.Data[index].IsLatest;
                    item.CreatedTime = serResult.Data[index].CreatedTime;
                    item.Creator = serResult.Data[index].Creator;
                    item.Value = serResult.Data[index].NewValue;
                    item.OldValue = serResult.Data[index].OldValue;
                    item.Reason = serResult.Data[index].Reason;
                    item.ColumnId = serResult.Data[index].ColumnId;

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetDataTableDataHistoryListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataTableDataHistoryListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataTableDataHistoryListParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }


        /// <summary>
        /// 数据主键列信息（pk1,pk2,pk3）,顺序要和值的顺序一样
        /// </summary>
        public string PKColumns { get; set; }

        /// <summary>
        /// 数据主键的值信息（pkvalue1,pkvalue2,pkvalue3),顺序要和名的顺序一样
        /// </summary>
        public string PKValues { get; set; }

        [Parameter]
        public string ColumnName { get; set; }
    }

    public class GetDataTableDataHistoryListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string DataId { get; set; }
            public Guid ColumnId { get; set; }

            public string ColumnName { get; set; }

            public string Value { get; set; }

            public string OldValue { get; set; }

            public DateTime CreatedTime { get; set; }

            public string Creator { get; set; }

            public bool IsLatest { get; set; }
            public int Reason { get; set; }
        }
    }
}
